package com.it.ssh.tools;

import java.util.Date;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class TimeTransform {
	/**
	 * String to date
	 * @param stime
	 * @return
	 */
	public  static  java.sql.Date Stringtodate(String stime){
		
		SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");
		//1.把字符串转换为util包的date
		java.util.Date utilDate = null;

		try {
			utilDate = format.parse(stime);
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//2.再把util包的date转换为sql包的date
		java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());
		return sqlDate;
	} 
	/**
	 * String to datetime
	 * @param stime
	 * @return
	 */
	public  static Timestamp Stringtodatetime(String stime){
		SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		//1.把字符串转换为util包的date
		java.util.Date utilDate = null;
		
		try {
			utilDate = format.parse(stime);
			
		} catch (ParseException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		//2.再把util包的date转换为sql包的date
		 Timestamp ts = new Timestamp(utilDate.getTime());  
		
		
		return ts;
	} 
	/**
	 * date to string
	 * @param date
	 * @return
	 */
  public static String Datetostring(Date  date){
	  SimpleDateFormat format =  new SimpleDateFormat("yyyy-MM-dd");
		//将data转换为string
		String sdate = format.format(date);
		return sdate;
  }	
	
	
}
